--[[
Author: your name
Date: 2020-10-30 15:32:28
LastEditTime: 2020-11-17 15:46:32
LastEditors: your name
Description: In User Settings Edit
FilePath: \Lua\Net\emailHandler.lua
--]]
require "proto/mail/message/ReqReadMailMessage"
require "proto/mail/message/ReqGetMailItemsMessage"
require "proto/mail/message/ReqDeleteMailMessage"
require "proto/mail/message/ReqAllMailMessage"


emailHandler = emailHandler or {};
G.serverData = G.serverData or {};

function emailHandler.GetEmailInfo(msg)
    --log("emailHandler.GetEmailInfo");
    --dataTable.PrintTable(msg);
    if msg.state == 0 then
        for i = 1, #msg.mails do
            logicMgr.EmailManager.addEmailInfo(msg.mails[i]);
        end
    end

end

--领取邮件附件结果 result 0 成功  >0 失败原因
function emailHandler.GetMailItemMsg(msg)
    --log("emailHandler.GetMailItemMsg");
    --dataTable.PrintTable(msg);
    if msg.state ~= 0 then
        log("领取邮件失败")
        return
    end
    if msg.result == 0 then
        logicMgr.EmailManager.SetMsg(msg);
        for k,v in pairs(msg.mailIdList) do
            logicMgr.EmailManager.updateEmailRewardState(tostring(v))
        end
        logicMgr.EmailManager.showAllNewHero(msg);
        
    else
        log("领取邮件失败"..tostring(msg.mailId))
    end
end

--通知新增一个邮件
function emailHandler.GetNewEmailMsg(msg)
    --log("emailHandler.GetNewEmailMsg");
    --dataTable.PrintTable(msg);
    if msg.state == 0 then
        logicMgr.EmailManager.addEmailInfo(msg.mail, true);
        FireEvent(Config.EventType.Fresh_Email);
    end
end

--通知邮件已删除
function emailHandler.ResDelEmailMsg(msg)
    --log("emailHandler.ResDelEmailMsg");
    --dataTable.PrintTable(msg);
    if msg.result == 0 then
        for k,v in pairs(msg.mailIdList) do
            logicMgr.EmailManager.deleteEmail(tostring(v))
        end
        FireEvent(Config.EventType.Fresh_Email);
    else
        log("删除邮件失败")
    end
end

--通知邮件已读
function emailHandler.ResReadEmailMsg(msg)
    --log("emailHandler.ResReadEmailMsg");
    --dataTable.PrintTable(msg);
    if msg.state == 0 then
        if msg.result == 0 then
            logicMgr.EmailManager.readEmail(tostring(msg.mailId));
            FireEvent(Config.EventType.Fresh_Email);
        else
            log("邮件已读通知失败"..msg.mailId.."邮件内容："..msg.mailContent)
        end
    end
end

--请求删除邮件
function emailHandler.reqDeleteMail(emailId)
    local req = ReqDeleteMailMessage.New();
    req.mailId = emailId;
    sendReqMessage(req);
end

--请求领取邮件奖励
function emailHandler.reqGetMailReward(emailId)
    local req = ReqGetMailItemsMessage.New();
    req.mailId = emailId;
    sendReqMessage(req);
end

--请求标记邮件已读
function emailHandler.reqReadMail(emailId)
    local req = ReqReadMailMessage.New();
    req.mailId = emailId;
    sendReqMessage(req);
end

--请求邮件信息
function emailHandler.ReqAllMail()
    local req = ReqAllMailMessage.New();
    sendReqMessage(req);
end